package config

import (
	"flag"
	"github.com/sirupsen/logrus"
	"gopkg.in/yaml.v3"
	"os"
	"sync"
)

var configFile = flag.String("f", "config.yaml", "the config file")

var once sync.Once

var config *Config

var log = logrus.New()

// LoadConfig 系统启动加载配置文件
func LoadConfig() *Config {
	if config == nil {
		once.Do(func() {
			flag.Parse()

			conf, err := os.ReadFile(*configFile)
			if err != nil {
				log.Errorf("读取配置文件失败：" + err.Error())
			}
			err = yaml.Unmarshal(conf, config)
			if err != nil {
				log.Errorf("解析配置文件失败：" + err.Error())
			}
		})
	}
	return config
}
